package com.chenjie.crud.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenjie.crud.dto.common.StationDto;
import com.chenjie.crud.dto.request.query.StationIdListReq;
import com.chenjie.crud.dto.request.query.StationIdReq;
import com.chenjie.crud.dto.request.query.StationReq;
import com.chenjie.crud.dto.request.query.StationUpdateReq;
import com.chenjie.crud.enums.ResultCodeEnum;
import com.chenjie.crud.eo.Station;
import com.chenjie.crud.exception.CMSException;
import com.chenjie.crud.service.StationService;
import com.chenjie.crud.mapper.StationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
* @author chenjie
* @description 针对表【station(岗位管理表)】的数据库操作Service实现
* @createDate 2022-12-10 14:22:27
*/
@Service
@Slf4j
public class StationServiceImpl extends ServiceImpl<StationMapper, Station>
    implements StationService{

    @Resource
    private StationMapper stationMapper;

    @Override
    @Transactional
    public Long createStation(StationReq stationReq) {
        log.info("createStation stationReq:{}",stationReq);
        StationDto stationDto = new StationDto();
        BeanUtil.copyProperties(stationReq,stationDto);
        Station station = new Station();
        BeanUtil.copyProperties(stationDto,station);
        int insert = stationMapper.insert(station);
        return station.getId();
    }

    @Override
    public Page<Station> selectStationByPage(StationReq stationReq) {
        Page<Station> page = new Page<>(stationReq.getPageNum(), stationReq.getPageSize());
//        OrderItem orderItem = stationReq.getOrderItem();
//        if(null!=orderItem){
//            page.addOrder(stationReq.getOrderItem());
//        }else{
//            page.addOrder(OrderItem.asc("id"));
//        }
        LambdaQueryWrapper<Station> stationLambdaQueryWrapper = Wrappers.<Station>lambdaQuery();
        if(StrUtil.isNotEmpty(stationReq.getStationCode())){
            stationLambdaQueryWrapper.eq(Station::getStationCode,stationReq.getStationCode());
        }
        if(StrUtil.isNotEmpty(stationReq.getName())){
            stationLambdaQueryWrapper.like(Station::getName,stationReq.getName());
        }
        if(null!=stationReq.getStationStatus()){
            stationLambdaQueryWrapper.eq(Station::getStationStatus,stationReq.getStationStatus());
        }
        Page<Station> userIPage = stationMapper.selectPage(page,stationLambdaQueryWrapper);
        return userIPage;
    }

    @Override
    public Station selectStation(Long id) {
        return stationMapper.selectById(id);
    }

    @Override
    @Transactional
    public Boolean updateStation(StationUpdateReq stationUpdateReq) {
        Long id = stationUpdateReq.getId();
        Station station = stationMapper.selectById(id);
        if(null==station){
            throw  new CMSException(ResultCodeEnum.NOT_EXIST_ERROR.getCode(),ResultCodeEnum.NOT_EXIST_ERROR.getMessage());
        }
        BeanUtil.copyProperties(stationUpdateReq,station);
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        int i = stationMapper.update(station,updateWrapper);
        return i>0;
    }

    @Override
    @Transactional
    public Boolean deleteStation(StationIdListReq stationIdListReq) {
        int i = stationMapper.deleteBatchIds(stationIdListReq.getIdList());
        return i>0;
    }
}




